﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using TuChongPicView.Common;

namespace TuChongPicView.Service
{
    public class HttpService<T>
    {
        private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";

        public static T post(String url, Dictionary<String, String> postData)
        {
            String resultStr = HttpUtils.HttpPost(url, postData);
            T result = default(T);
            if (resultStr != null)
            {
                try
                {
                    result = JsonConvert.DeserializeObject<T>(resultStr);
                }
                catch (Exception e)
                {
                    log.Error(e.Message);
                }
            }
            return result;
        }

        public static T get(String url, Dictionary<String, String> postData)
        {
            log.Debug("request url :" + url);
            WebClient webClient = new WebClient();
            String resultStr = webClient.DownloadString(url);
            T result = default(T);
            if (resultStr != null)
            {
                try
                {
                    result = JsonConvert.DeserializeObject<T>(resultStr);
                }
                catch (Exception e)
                {
                    log.Error(e.Message);
                }
            }
            return result;
        }
    }
}
